package com.spider.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hqj.ReturnUtil;
import com.spider.domain.Spider;
import com.spider.service.QuartzServiceI;
import com.spider.service.SpiderServiceI;
import com.spider.task.ScheduleOfTask;
import com.spider.task.SpiderManTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.*;

@Controller
@RequestMapping("/sc")
public class SpiderController extends BaseController {

    @Resource(name = "spiderServiceI")
    private SpiderServiceI ssi;

    @Autowired
    private QuartzServiceI qsi;

    @Autowired
    private ScheduleOfTask sot;

    @RequestMapping("/index")
    public String index(){
        return prefix + "/spider/spiderList/spiderList" + suffix;
    }

    @RequestMapping("/find")
    @ResponseBody
    public Map<String,Object> find(Spider s){
        QueryWrapper<Spider> qw = new QueryWrapper<>();
        List<Spider> list = ssi.list(qw);
        Map<String,Object> map = new HashMap<>();
        map.put("code",0);
        map.put("msg","数据查询成功");
        map.put("data",list);
        map.put("size",list.size());
        return map;
    }

    @RequestMapping("/addPage")
    public String addPage(){
        return prefix + "/spider/spiderList/addSpider" + suffix;
    }
    @RequestMapping("/editPage/{id}")
    public String editPage(@PathVariable("id") int id, ModelMap mmap){
        Spider s = ssi.getById(id);
        if(s!=null){
            mmap.addAttribute("s",s);
        }
        return prefix + "/spider/spiderList/editSpider" + suffix;
    }
    @RequestMapping("/addOrEdit")
    @ResponseBody
    public Map<String,Object> addOrEdit(Spider s){
        s.setCreateTime(new Date());

        boolean b = ssi.saveOrUpdate(s);
        if(b){
            String cron = s.getCron();
            String jobName = "spiderJob"+s.getId();
            String jobGroup = "spiderJobGroup";
            String triName = "spiderTri"+s.getId();
            String triGroup = "spiderTriGroup";
            try {
                qsi.init(cron,Integer.parseInt(s.getIsUse()),
                        jobName,jobGroup,
                        triName,triGroup,
                        SpiderManTask.class.getName());
            }catch (Exception e){
                System.out.println("定时任务启动失败");
            }
        }
        return ReturnUtil.returnMap(b,s.getId()==null?"保存":"保存");
    }

    @RequestMapping("/remove/{id}")
    @ResponseBody
    public Map<String,Object> remove(@PathVariable(value = "id") int id){
        boolean b = ssi.removeById(id);
        return ReturnUtil.returnMap(b,"删除");
    }


    @RequestMapping("/test")
    @ResponseBody
    public Map<String,List<String>> test(Spider s){
        Map<String,List<String>> map = ssi.test(s);
        if(map==null){
            map = new HashMap<>();
            List<String> list = new ArrayList<>();
            list.add("true");
            map.put("is",list);
        }

        return map;
    }

    @RequestMapping("/runTask")
    @ResponseBody
    public String runTask(){

        sot.execute();
        return "123";
    }

}
